package mysprintmvc;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        int actionIndex = uri.indexOf(".action");
        String path = uri.substring(1,actionIndex);
        Object actionObj = context.getBean(path);
        String oper = request.getParameter("oper");
        if (oper==null){
            oper = "index";
        }
        try {
            Method[] methods = actionObj.getClass().getDeclaredMethods();
            for (Method md:methods){
                if (md.getName().equals(oper)){
                  //invoke之前，获取参数，创建session
                    int parameterCount = md.getParameterCount();
                    //获取m方法的所有参数
                    Parameter[] parameters = md.getParameters();
                    Object[] paramObjs = new Object[parameterCount];
                    for (int i = 0; i <parameters.length ; i++) {
                        Parameter param = parameters[i];
                        //获取参数名称
                        String paramName = param.getName();
                        if ("session".equals(paramName)){
                            HttpSession session = request.getSession();
                            paramObjs[i] = session;
                        }else {
                            String paramValueStr = request.getParameter(paramName);
                            Object paramValue = paramValueStr;
                            String typeName = param.getType().getName();
                            if ("java.lang.Integer".equals(typeName)){
                                if (paramValueStr != null){
                                    paramValue = Integer.parseInt(paramValueStr);
                                }
                            }
                            paramObjs[i] = paramValue;
                        }
                    }
                    Object viewObj =md.invoke(actionObj,paramObjs);
                        ViewResolver.getInstance().resolverView(request,response,viewObj);
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

